package com.hepinghospital.user.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.hepinghospital.user.R;
import com.hepinghospital.user.util.ToastUtil;
import com.hepinghospital.user.util.UtilString;
import com.rest.response.PrescriptionResponse;

import org.jetbrains.annotations.NotNull;

import java.util.List;


/**
 * Created by HP on 2020/3/2.
 */

public class AtyMedicineAdapter extends RecyclerView.Adapter<AtyMedicineAdapter.Holder> {

    private Context context;
    private List<PrescriptionResponse.Prescription> list;

    public AtyMedicineAdapter(Context context, List<PrescriptionResponse.Prescription> list) {
        this.context = context;
        this.list = list;
    }

    @NotNull
    @Override
    public Holder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
        Holder myHolder;
        myHolder = new Holder(LayoutInflater.from(context).inflate(R.layout.item_aty_medicine, parent, false));
        return myHolder;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(Holder holder, final int position) {
        holder.tv_medicine_name.setText(list.get(position).name);
        holder.tv_medicine_num.setText(list.get(position).drugDose + "  " + list.get(position).drugUsing + "  " + list.get(position).drugChannel);
//        holder.tv_medicine_price.setText(list.get(position).expense + "*" + list.get(position).drugNumber + "=" + list.get(position).totalExpense + "元");
        holder.tv_medicine_price.setText(list.get(position).expense + "元");
        holder.tv_number.setText(list.get(position).drugNumber + list.get(position).outPatientUnit);
        holder.tv_medicine_name.setOnClickListener(v -> {
            if (!TextUtils.isEmpty(list.get(position).instructionUrl)){
                Uri uri = Uri.parse(list.get(position).instructionUrl);
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                intent.setData(uri);
                context.startActivity(intent);
            }else {
                ToastUtil.showToast(context,"说明书地址为空");
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class Holder extends RecyclerView.ViewHolder {

        TextView tv_medicine_name, tv_medicine_num, tv_medicine_price, tv_number;

        Holder(View itemView) {
            super(itemView);
            tv_medicine_name = itemView.findViewById(R.id.tv_medicine_name);
            tv_medicine_num = itemView.findViewById(R.id.tv_medicine_num);
            tv_medicine_price = itemView.findViewById(R.id.tv_medicine_price);
            tv_number = itemView.findViewById(R.id.tv_number);
        }
    }
}
